using System.Collections.Generic;
using System;
namespace Slaks.Graphs.Algorithms
{
	/// <summary>
	/// Summary description for WordnetGraphDistance.
	/// </summary>
	public class WordnetGraphDistance : GraphDistanceBase
	{
        public WordnetGraphDistance() : base ()
        {
        }

        public WordnetGraphDistance(Graph one, Graph two) 
            : base ( one, two )
        {
        }        

        public override double GetDistance()
		{
			double commonDistance = 0;
			long oneSize = one.GetVertexCount();
			long twoSize = two.GetVertexCount();

			one.SortVertices();
			two.SortVertices();

			int compare;
			VertexList oneList = one.GetVertexList();
			VertexList twoList = two.GetVertexList();

			#region find vertices

			//find intersections
			int i = 0, j = 0;
			int count = 0;
			while (i < oneList.Count && j < twoList.Count)
			{
				compare = oneList[i].CompareTo(twoList[j]);
				if (compare < 0) i++;
				else if (compare == 0)
				{
					commonDistance += oneList[i].Frequency*twoList[j].Frequency;
					i++;
					j++;
					++count;
				}
				else j++;
			}

			#endregion

			return 1 - commonDistance/(commonDistance + oneSize + twoSize - count - count);
		}
	}
}